package cn.tedu._06article.controller;
import cn.tedu._06article.mapper.mapper;
import cn.tedu._06article.base.response.code;
import cn.tedu._06article.base.response.jsonresoult;
import cn.tedu._06article.pojo.dto.insuser;
import cn.tedu._06article.pojo.dto.login;
import cn.tedu._06article.pojo.entity.user;
import cn.tedu._06article.pojo.vo.loginvo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Tag(name="用户管理")
@Slf4j
@RestController
@RequestMapping("/v1/user")
public class ucon
{
    @Autowired
    private mapper mapper;

    @PostMapping("/add")
    @Operation(summary = "添加用户")
    @ApiOperationSupport(order = 10)
public jsonresoult add(@RequestBody @Validated insuser insuser)
{
log.debug("开始处理【添加用户】的请求，参数：{}",insuser);

int bb=mapper.select(insuser);
if(bb!=0)
{return new jsonresoult(code.USERNAME_EXISTS);}
    user user=new user();
BeanUtils.copyProperties(insuser,user);
user.setCreatetime(new Date());
mapper.insert(user);
return new jsonresoult(code.SUCCESS);
}

    @PostMapping("/login")
    @Operation(summary = "登录功能")
    @ApiOperationSupport(order = 20)
    public jsonresoult login(@RequestBody login login ,HttpSession session)
    {
        log.debug("登录"+ login);
        loginvo vo=mapper.selectlogin(login);
        if(vo==null)
        {
            return new jsonresoult(code.USERNAME_PASSWORD_ERROR);
        }
        session.setAttribute("user",vo);
        return jsonresoult.success(vo);
    }

}
